package cn.com.yusys.yusp.admin.service;

import cn.com.yusys.yusp.admin.domain.AdminSmDataAuth;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmAuthRecoMapper;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmDataAuthMapper;
import cn.com.yusys.yusp.commons.mapper.CommonMapper;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.commons.util.DateUtil;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @项目名称: yusp-admin
 * @类名称: AdminSmDataAuthService
 * @类描述: 数据权限配置
 * @功能描述:
 * @创建人: weimei1@yusys.com.cn
 * @创建时间: 2017-12-19 11:32
 * @修改备注:
 * @修改记录: 修改时间 修改人员 修改原因
 *        -------------------------------------------------------------
 * @version 1.0.0
 * @Copyright (c) 2017宇信科技-版权所有
 */
@Service
public class AdminSmDataAuthService extends CommonService {

    @Autowired
    private AdminSmDataAuthMapper adminSmDataAuthMapper;

    @Autowired
    private AdminSmAuthRecoMapper adminSmAuthRecoMapper;

    @Override
    protected CommonMapper<?> getMapper() {
        return this.adminSmDataAuthMapper;
    }

    /**
     * @方法名称: getDataAuthInfo
     * @方法描述: 查询数据权限表
     * @参数与返回说明:
     * @算法描述:
     */
    @Transactional(readOnly = true)
    public List<Map<String, Object>> getDataAuthInfo(QueryModel model) {
        PageHelper.startPage(model.getPage(), model.getSize());
        if (model.getCondition().containsKey("authTmplName")) {
            model.getCondition().put("authTmplName", "%" + model.getCondition().get("authTmplName") + "%");
        }
        if (model.getCondition().containsKey("sqlString")) {
            model.getCondition().put("sqlString", "%" + model.getCondition().get("sqlString") + "%");
        }
        List<Map<String, Object>> list = adminSmDataAuthMapper.getDataAuthInfo(model);
        PageHelper.clearPage();
        return list;
    }

    /**
     * @方法名称: saveDatuAuth
     * @方法描述: 新增数据权限信息
     * @参数与返回说明:
     * @算法描述:
     */
    public int createDataAuth(AdminSmDataAuth adminSmDataAuth) {
        adminSmDataAuth.setLastChgDt(DateUtil.formatDateTimeByDef());
        return this.insertSelective(adminSmDataAuthMapper, adminSmDataAuth);
    }

    /**
     * @方法名称: editDatuAuth
     * @方法描述: 修改数据权限信息
     * @参数与返回说明:
     * @算法描述:
     */
    public int editDatuAuth(AdminSmDataAuth adminSmDataAuth) {
        adminSmDataAuth.setLastChgDt(DateUtil.formatDateTimeByDef());
        return this.updateSelective(adminSmDataAuthMapper, adminSmDataAuth);
    }

    /**
     * @方法名称: getAuthTmpl
     * @方法描述: 查询控制点可用数据权限模板
     * @参数与返回说明:
     * @算法描述:
     */
    @Transactional(readOnly = true)
    public List<Map<String, Object>> getAuthTmpl(QueryModel model) {
        PageHelper.startPage(model.getPage(), model.getSize());
        if (model.getCondition().get("contrId") == null || model.getCondition().get("contrId").equals("")) {
            return null;
        }
        if (model.getCondition().containsKey("authTmplName")) {
            if (model.getCondition().get("authTmplName") != null
                    && !model.getCondition().get("authTmplName").equals("")) {
                model.getCondition().put("authTmplName", "%" + model.getCondition().get("authTmplName") + "%");
            }
        }
        List<Map<String, Object>> list = adminSmDataAuthMapper.getAuthTmpl(model);
        return list;
    }

    /**
     * @方法名称: deleteDataAuth
     * @方法描述: 删除数据权限信息,同时删除其授权信息
     * @参数与返回说明:
     * @算法描述:
     */
    @Transactional(readOnly = false, rollbackFor = { Exception.class, RuntimeException.class })
    public int deleteDataAuth(String ids) {
        int n = 0;
        if (ids != null && !"".equals(ids)) {
            String[] authId = ids.split(",");
            this.adminSmAuthRecoMapper.deleteDataAuth(authId);
            for (int i = 0; i < authId.length; i++) {
                if (!"".equals(authId[i])) {
                    n = n + this.adminSmDataAuthMapper.deleteByPrimaryKey(authId[i]);
                }
            }
        }
        return n;
    }

    /**
     * @方法名称: getTree
     * @方法描述: 左侧树查询
     * @参数与返回说明:
     * @算法描述:
     */
    @Transactional(readOnly = true)
    public List<Map<String, Object>> getTree(String nodeId) {
    	List<Map<String, Object>> list =new ArrayList<Map<String,Object>>();
    	if(nodeId==null) { // 非懒加载
    		list = adminSmDataAuthMapper.getTree();
    	}else if(nodeId.equals("0")) {
    		list = adminSmDataAuthMapper.getAllFuncMods();
    	}else {
    		list = adminSmDataAuthMapper.getFunWithContrlByModId(nodeId);
    	}
        return list;
    }
}
